package com.feeyo.groundservice.composewidget.usage

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.feeyo.groundservice.composewidget.usage.base.BaseComposeActivity
import com.feeyo.groundservice.composewidget.view.refreshlayout.PullToRefreshLayout
import com.feeyo.groundservice.composewidget.view.refreshlayout.RefreshContentStateEnum
import com.feeyo.groundservice.composewidget.view.refreshlayout.VerticalRefreshableLayout
import com.feeyo.groundservice.composewidget.view.refreshlayout.rememberRefreshLayoutState
import com.feeyo.groundservice.composewidget.view.util.LanguageType
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *作者：QLH on 2024-09-13
 *描述：
 */
class RefreshActivity : BaseComposeActivity() {

    private val language = mutableStateOf(LanguageType.ZH)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column {
                PullRefreshPage()
                VerticalSpace(20.dp)
                PullToRefreshBoxPage()
                VerticalSpace(20.dp)
                PullToRefreshAndLoadMore()
                VerticalSpace(20.dp)
                Button(
                    onClick = {
                        if (language.value == LanguageType.ZH) {
                            language.value = LanguageType.EN
                        } else {
                            language.value = LanguageType.ZH
                        }
                    }
                ) {
                    Text(text = "切换语言")
                }
            }
        }
    }

    @Composable
    private fun PullRefreshPage() {
        PullToRefreshLayout(
            modifier = Modifier.fillMaxWidth().height(200.dp),
            currentLanguage = language.value,
            refreshLayoutState = rememberRefreshLayoutState {
                GlobalScope.launch {
                    delay(2000)
                    this@rememberRefreshLayoutState.setRefreshState(
                        RefreshContentStateEnum.Stop
                    )
                }
            },

            ) {
            val items = (0..20).map { "Item $it" }
            LazyColumn(
                modifier = Modifier.fillMaxSize().background(Color.Red)
            ) {
                items(items) {
                    Text(text = it)
                }
            }
        }
    }

    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    private fun PullToRefreshBoxPage() {
        val isRefreshing = remember { mutableStateOf(false) }
        PullToRefreshBox(
            modifier = Modifier.fillMaxWidth().height(200.dp),
            isRefreshing = isRefreshing.value,
            onRefresh = {
                isRefreshing.value = true
                GlobalScope.launch {
                    delay(2000)
                    isRefreshing.value = false
                }
            },
        ) {
            val items = (0..20).map { "Item $it" }
            LazyColumn(
                modifier = Modifier.fillMaxSize().background(Color.Green)
            ) {
                items(items) {
                    Text(text = it)
                }
            }
        }
    }

    @Composable
    fun PullToRefreshAndLoadMore() {

        VerticalRefreshableLayout(
            modifier = Modifier.fillMaxWidth().height(200.dp),
            currentLanguage = language.value,
            topRefreshLayoutState = rememberRefreshLayoutState {
                GlobalScope.launch {
                    delay(2000)
                    this@rememberRefreshLayoutState.setRefreshState(
                        RefreshContentStateEnum.Stop
                    )
                }
            },
            bottomRefreshLayoutState = rememberRefreshLayoutState {
                GlobalScope.launch {
                    delay(2000)
                    this@rememberRefreshLayoutState.setRefreshState(
                        RefreshContentStateEnum.Stop
                    )
                }
            },
            bottomIsLoadFinish = false,
        ) {
            val items = (0..20).map { "Item $it" }
            LazyColumn(
                modifier = Modifier.fillMaxSize().background(Color.Blue)
            ) {
                items(items) {
                    Text(text = it)
                }
            }
        }
    }
}


